﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Visual_CRUD_Sequence_ImportfromFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        verificaUser();
    }
    protected void verificaUser()
    {
        VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
        if (!(usuariologado.acessgroup == 1 || usuariologado.acessgroup == 2))
        {
            HttpContext.Current.Session["exception"] = "You are not authorized!";
            Response.Redirect("~/Visual/Errors/error.aspx");
        }
    }
    protected void btnSelectFile_Click(object sender, EventArgs e)
    {
        FileUpload.Visible = true;
    }

    protected void btnImportFiles_Click(object sender, EventArgs e)
    {
        if (FileUpload.HasFile)
        {
            if (FileUpload.PostedFile.ContentType == "text/xml")
            {
                try
                {

                    //Obtém o conteúdo do arquivo e passa para o DataSet.
                    Stream file = FileUpload.FileContent;
                    DataSet ds = new DataSet();
                    ds.ReadXml(file);
                    //Obtém usuário logado e passa para o zeseeker inserir junto com a tabela de sequencias do XML.
                    VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
                    if (ds.Tables[0].TableName == "INSDSeq")
                    {

                        VSDBM.Code.functions.zeseeker.insertFromFile(ds.Tables[0], usuariologado.id);

                        //Exibe informações do arquivo selecionado.
                        lblResult.Text = string.Format("File: {0} - Size: {1} kb - Content Type: '{2}' - Amount of sequences added: {3}  ",
                        FileUpload.PostedFile.FileName, FileUpload.PostedFile.ContentLength,
                        FileUpload.PostedFile.ContentType, ds.Tables[0].Rows.Count);
                        
                        
                    }
                    else
                    {
                        HttpContext.Current.Session["exception"] = "XML Incorreto";
                        Response.Redirect("../../../Visual/Errors/error.aspx");
                    }

                }
                catch (Exception ex)
                {
                    HttpContext.Current.Session["exception"] = ex.Message;
                    Response.Redirect("../../../Visual/Errors/error.aspx");
                }
                //Manda pra DONE.(Não colocar dentro do Try...)
                HttpContext.Current.Session["done"] = lblResult.Text;
                Response.Redirect("../../../Visual/Done/Done.aspx");

            }
            else
            {
                HttpContext.Current.Session["exception"] = "The selected file is not an valid '.xml' file. ";
                Response.Redirect("../../../Visual/Errors/error.aspx");
            }
        }
        else
        {
            HttpContext.Current.Session["exception"] = "ERROR: You Should select a file to upload";
            Response.Redirect("../../../Visual/Errors/error.aspx");

        }
    }
}